<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const mySqrt = function (x) {
            if (x < 2) return x
            let l = 1, mid, r = Math.floor(x / 2);
            while (l <= r) {
                mid = Math.floor(l + (r - l) / 2)
                if (mid * mid === x) return mid
                if (mid * mid < x) {
                    l = mid + 1
                } else {
                    r = mid - 1 
                }
            }
            return r
        }

        console.log(mySqrt(7));
    </script>
</body>

</html>